package com.sxfq.loansupermarket.backsystem.modular.theTest.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroUser;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.dao.TheTestDao;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.SearchTheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.entity.TheTestInfo;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.ITheTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TheTestServiceImpl implements ITheTestService {

    @Autowired
    private TheTestDao theTestDao;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<TheTestInfo> selectPageListByQueryGetMap(Page<TheTestInfo> page, SearchTheTestInfo searchTheTestInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("userId", ShiroKit.getUser().getId());
        return theTestDao.getQueryPage(page, params);
    }

    @Transactional
    public void UpdateWithTheTestInfo(TheTestInfo theTestInfo) throws Exception {
        ShiroUser shiroUser = ShiroKit.getUser();
        theTestInfo.setUpdatePeople(shiroUser.getId().toString());
        theTestInfo.setUpdateTime(DateTimeKit.now());
        theTestDao.updateById(theTestInfo);
    }

    @Transactional
    public int selectTestStatus(String theTestId, int userId) {
        return theTestDao.selectTestStatusCount(theTestId, userId);
    }

    @Transactional
    public int selectSameTestNameCount(String theTestName) throws Exception {
        int count = theTestDao.selectSameTestNameCount(theTestName);
        return count;
    }

    @Transactional
    public int selectSameTestNameCountForUpdate(String id, String theTestName) throws Exception {
        int count = theTestDao.selectSameTestNameCountForUpdate(id, theTestName);
        return count;
    }

    @Transactional
    public int insert(TheTestInfo info) throws Exception {
        String sql = "insert into the_test_info" +
                " (id,scores_of_adoption,total_score,type_of_question,number_of_question,scores_of_question,the_test_name,pass_word,time_length,create_people,update_people,create_time,update_time,remark)" +
                " value ('" + CommUtils.getUUID() + "'," + info.getScoresOfAdoption() + ",'" + info.getNumberOfQuestion() * info.getScoresOfQuestion() + "','" + info.getTypeOfQuestion() + "','" + info.getNumberOfQuestion() + "','" + info.getScoresOfQuestion() + "','" + info.getTheTestName().trim() + "','" + info.getPassWord().trim() + "','" + info.getTimeLength().trim() + "','" + ShiroKit.getUser().getName() +
                "','" + ShiroKit.getUser().getName() + "','" + DateTimeKit.now() + "','" + DateTimeKit.now() + "','" + info.getRemark().trim() + "')";
        return sqlMapper.insert(sql);
    }

    public TheTestInfo getTheTestOne(String theTestId) {
        String sql = "select id,scores_of_adoption,total_score,type_of_question,number_of_question,scores_of_question,the_test_name,pass_word,remark,time_length from the_test_info  where id= '" + theTestId + "'";
        TheTestInfo theTestInfo = sqlMapper.selectOne(sql, TheTestInfo.class);
        return theTestInfo;
    }

    public void delete(String id) {
        String sql = "delete from the_test_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
    }
}
